//
//  ReportViewController.m
//  MoneyLog
//
//  Created by wicresoft on 6/1/11.
//  Copyright 2011 wicresoft. All rights reserved.
//

#import "ReportViewController.h"
#import "PieChartViewController.h"
#import "MonthReportViewController.h"

@implementation ReportViewController
@synthesize _tableView;
@synthesize dataSourceArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [_tableView release];
    [dataSourceArray release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.    
    self.title = @"报表";
    
    self.dataSourceArray = [[NSMutableArray alloc] initWithObjects:@"收支类别",@"按月统计", nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - * Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString *title = nil;
	
	return title;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.dataSourceArray count];}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ReportCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.textLabel.text = [self.dataSourceArray objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

//// Override to support conditional editing of the table view.
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // Return NO if you do not want the specified item to be editable.
//    return YES;
//}
//
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleNone;
//}
//
//// Override to support editing the table view.
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        // Delete the row from the data source
//    }   
//    else if (editingStyle == UITableViewCellEditingStyleInsert) {
//        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
//    }   
//    
//}
//
//- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
//{
//	NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);
//}
//
//- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
//{
//	NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);
//}

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - * Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIBarButtonItem *backItem=[[UIBarButtonItem alloc] 
                               initWithTitle:@"返回" 
                               style:UIBarButtonItemStyleBordered 
                               target:nil 
                               action:nil];

    int year,month,day;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate new]];
    year = [dateComponents year];
    month = [dateComponents month];
    day = [dateComponents day];
    [calendar release];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            PieChartViewController *controller = [[PieChartViewController alloc] init];
            controller.controllerState = kPieChartControllerAll;
           
            MyDate *startDate=[[MyDate alloc] init];
            startDate.year =year;
            startDate.month = month;
            startDate.day = 1;//day;
        
            controller.searchStartDate = startDate;
            [startDate release];
            
            MyDate *endDate=[[MyDate alloc] init];
            endDate.year =year;
            endDate.month = month;
            endDate.day = 31;//day;
            
            controller.searchEndDate = endDate;
            [endDate release]; 
            
            controller.searchString = @" 1=1 ";
            
            self.navigationItem.backBarButtonItem = backItem;
            [self.navigationController pushViewController:controller animated:YES];
            [controller release];
            break;
        }
        case 1:
        {
            MonthReportViewController *controller = [[MonthReportViewController alloc] init];
            MyDate *startDate=[[MyDate alloc] init];
            startDate.year =year;
            startDate.month = 1;
            
            controller.searchStartDate = startDate;
            [startDate release];
            
            MyDate *endDate=[[MyDate alloc] init];
            endDate.year =year;
            endDate.month = 12;
            
            controller.searchEndDate = endDate;
            [endDate release]; 
            
            self.navigationItem.backBarButtonItem = backItem;
            [self.navigationController pushViewController:controller animated:YES];
            [controller release];
            break;
        }
        default:
            break;
    }
    
    [backItem release];
    
}

#pragma mark - * Actions


@end
